<?php
/**
 * Created by PhpStorm.
 * User: Hanson
 * Date: 2016/9/25
 * Time: 23:08
 */

namespace App\Repository;


use App\Model\User;

class UserRepository
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * id查找用户
     *
     * @param $id
     * @return mixed
     */
    public function getUser($id)
    {
        return $this->user->find($id);
    }

    /**
     * openId查找用户
     *
     * @param $openId
     * @return mixed
     */
    public function getUserByOpenId($openId)
    {
        return $this->user->where('open_id', $openId)->first();
    }

    /**
     * 用户是否存在
     *
     * @param $array
     * @return mixed
     */
    public function exist($array)
    {
        return $this->user->withTrashed()->where($array)->first();
    }

    /**
     * number查找用户
     *
     * @param $number
     * @return mixed
     */
    public function getUserByNumber($number)
    {
        return $this->user->where('number', $number)->first();
    }

    public function getUserWithClass($number)
    {
        return $this->user->with('class')->where('number', $number)->first();
    }

    /**
     * number查找用户
     *
     * @param $number
     * @return mixed
     */
    public function getUserByNumberOrFail($number)
    {
        return $this->user->where('number', $number)->firstOrFail();
    }

    public function updateUserPhone($openId, $shortPhone, $cellphone)
    {
        return $this->user->where('open_id', $openId)->update(['short_phone' => $shortPhone, 'cellphone' => $cellphone]);
    }

    /**
     * 手动更改用户姓名学号
     *
     * @param $openId
     * @param $name
     * @param $number
     */
    public function updateUser($openId, $name, $number)
    {
        $user = $this->user->firstOrCreate(['open_id' => $openId]);

        $user->update(['name' => $name, 'number' => $number]);
    }

    public function update($id, $array)
    {
        return $this->user->find($id)->update($array);
    }

    public function firstOrCreate(array $array)
    {
        return $this->user->firstOrCreate($array);
    }

    /**
     * 获取异常用户
     *
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getExceptionUsers()
    {
        return $this->user->where('name', null)->where('number', '!=', null)->get();
    }

    /**
     * 用户取消关注
     *
     * @param $openId
     * @return bool|null
     */
    public function delete($openId)
    {
        return $this->user->where('open_id', $openId)->first()->delete();
    }

    /**
     * 找出某学号的同班同学
     *
     * @param $number
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getClassmates($number)
    {
        $user = User::where('number', $number)->first();

        return User::where('class_id', $user->class_id)->get();
    }

    /**
     * 通过班级名称找学生
     *
     * @param $name
     * @return mixed
     */
    public function getClassmatesByClassName($name)
    {
        return User::whereHas('class', function($query) use ($name){
            $query->where('name', $name);
        })->get();
    }

    public function getSameGradeMajor($grade, $major)
    {
        return User::where('major', $major)->where('grade', $grade)->get();
    }

    public function getSameGradeCollege($grade, $college)
    {
        return User::whereHas('class', function($query) use ($college){
            $query->where('college', $college);
        })->where('grade', $grade)->get();
    }

    public function isClassmate($number1, $number2)
    {
        $class1 = $this->user->where('number', $number1)->first()->class->name;

        $user = $this->user->where('number', $number2)->first();

        if(!$user){

            $info = \BindService::getInfo($number2);

            if($info){
                $class2 = $info['class'];
            }else{
                return false;
            }
        }else{
            $class2 = $user->class->name;
        }

        return $class1 === $class2;
    }


    /**
     * 绑定用户
     *
     * @return mixed
     */
    public function getBindUsers($field=null)
    {
        return $this->user->where('number', '!=', null)->with('class')->get($field);

    }

    
    public function updateUserFromRepair($openId, $platform, $cellPhone, $shortPhone, $address)
    {
        $user = $this->getUserByOpenId($openId);

        $user->cellphone = $cellPhone;
        $user->short_phone = $shortPhone;

        if($platform == '1'){
            $user->dormitory_address = $address;
        }


        $result = $user->save();

        return $result;
    }

    /**
     * 用户总数
     *
     * @return int
     */
    public function getUserCount()
    {
        return $this->user->count();
    }

    /**
     * 绑定用户总数
     *
     * @return int
     */
    public function getBindCount()
    {
        return $this->user->where('number', '!=', null)->count();
    }
}